package at.codebase.web.templates;
import static at.codebase.web.util.i18n.I18NFactory.i18n;
import at.codebase.core.LinkCreator;
import at.codebase.db.entities.EItemFeedback;
import at.codebase.db.entities.EItemFeedback.EItemFeedbackRating;
import at.codebase.db.entities.EUser;
import at.codebase.security.SecurityUtils;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.i10n.L10N;
import at.codebase.web.util.i18n.I18NLocale;
import at.codebase.web.util.servlet.SecureServlet.UserLevel;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

/**
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 *
 */
public class ItemDetailsFeedback extends Template {

	private static String TEMPLATE_URL = "item/site_details_feedback.html";

	public ItemDetailsFeedback(RequestInformation requestInfo, UserLevel level, EItemFeedback itemFeedback) {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL));
		init(requestInfo, level, itemFeedback);
	}

	private void init(RequestInformation requestInfo, UserLevel level, EItemFeedback itemFeedback) {
		EUser user = itemFeedback.getAuthor().get();
		
		level.name();
		
		I18NLocale usLoc = requestInfo.getLocale();
		setHTML("authorTime",
				i18n(usLoc).string("itemDetails_feedback_authorTime",
						LinkCreator.userLink(user.getId(), user.getUsername()),
						L10N.formatDate(usLoc, itemFeedback.getTimeCreate()),
						L10N.formatTime(usLoc, itemFeedback.getTimeCreate())));
		setContent(itemFeedback.getFeedbackContent());
		setRating(itemFeedback.getFeedbackRating());
		if(level.equals(UserLevel.ADMIN)){
			setHTML("adminRemoveLink",
					"<a class=\"feedbackRemoveLink\" feedbackId=\""+itemFeedback.getId()+"\" style=\"float:left;display:inline;padding-right: 5px;\" href=\"#removeFeedback\">"
							+ i18n(requestInfo.getLocale()).string("itemDetails_feedback_admin_removeLink")
							+ "</a>");
		}
	}
	
	private void setContent(String text){
		setHTML("feedbackText", SecurityUtils.sanatize(text).replace("\n", "<br>"));
	}
	
	private void setRating(EItemFeedbackRating rating){
		setTemplate("rubyRatingBar", new RubyRatingBar(rating));
	}
	
}
